home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zfont.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  13.1 KB  |  504 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zfont.c,v 1.2 2000/09/19 19:00:53 lpd Exp $ */
  20. /* Generic font operators */
  21. #include "ghost.h"
  22. #include "oper.h"
  23. #include "gsstruct.h"        /* for registering root */
  24. #include "gzstate.h"        /* must precede gxdevice */
  25. #include "gxdevice.h"        /* must precede gxfont */
  26. #include "gxfont.h"
  27. #include "gxfcache.h"
  28. #include "bfont.h"
  29. #include "ialloc.h"
  30. #include "iddict.h"
  31. #include "igstate.h"
  32. #include "iname.h"        /* for name_mark_index */
  33. #include "isave.h"
  34. #include "store.h"
  35. #include "ivmspace.h"
  36.  
  37. /* Forward references */
  38. private int make_font(P2(i_ctx_t *, const gs_matrix *));
  39. private void make_uint_array(P3(os_ptr, const uint *, int));
  40.  
  41. /* The (global) font directory */
  42. gs_font_dir *ifont_dir = 0;    /* needed for buildfont */
  43.  
  44. /* Mark a glyph as a PostScript name (if it isn't a CID). */
  45. bool
  46. zfont_mark_glyph_name(gs_glyph glyph, void *ignore_data)
  47. {
  48.     return (glyph >= gs_min_cid_glyph || glyph == gs_no_glyph ? false :
  49.         name_mark_index((uint) glyph));
  50. }
  51.  
  52. /* Initialize the font operators */
  53. private int
  54. zfont_init(i_ctx_t *i_ctx_p)
  55. {
  56.     ifont_dir = gs_font_dir_alloc2(imemory, &gs_memory_default);
  57.     ifont_dir->ccache.mark_glyph = zfont_mark_glyph_name;
  58.     return gs_register_struct_root(imemory, NULL, (void **)&ifont_dir,
  59.                    "ifont_dir");
  60. }
  61.  
  62. /* <font> <scale> scalefont <new_font> */
  63. private int
  64. zscalefont(i_ctx_t *i_ctx_p)
  65. {
  66.     os_ptr op = osp;
  67.     int code;
  68.     double scale;
  69.     gs_matrix mat;
  70.  
  71.     if ((code = real_param(op, &scale)) < 0)
  72.     return code;
  73.     if ((code = gs_make_scaling(scale, scale, &mat)) < 0)
  74.     return code;
  75.     return make_font(i_ctx_p, &mat);
  76. }
  77.  
  78. /* <font> <matrix> makefont <new_font> */
  79. private int
  80. zmakefont(i_ctx_t *i_ctx_p)
  81. {
  82.     os_ptr op = osp;
  83.     int code;
  84.     gs_matrix mat;
  85.  
  86.     if ((code = read_matrix(op, &mat)) < 0)
  87.     return code;
  88.     return make_font(i_ctx_p, &mat);
  89. }
  90.  
  91. /* <font> setfont - */
  92. int
  93. zsetfont(i_ctx_t *i_ctx_p)
  94. {
  95.     os_ptr op = osp;
  96.     gs_font *pfont;
  97.     int code = font_param(op, &pfont);
  98.  
  99.     if (code < 0 || (code = gs_setfont(igs, pfont)) < 0)
  100.     return code;
  101.     pop(1);
  102.     return code;
  103. }
  104.  
  105. /* - currentfont <font> */
  106. private int
  107. zcurrentfont(i_ctx_t *i_ctx_p)
  108. {
  109.     os_ptr op = osp;
  110.  
  111.     push(1);
  112.     *op = *pfont_dict(gs_currentfont(igs));
  113.     return 0;
  114. }
  115.  
  116. /* - cachestatus <mark> <bsize> <bmax> <msize> <mmax> <csize> <cmax> <blimit> */
  117. private int
  118. zcachestatus(i_ctx_t *i_ctx_p)
  119. {
  120.     os_ptr op = osp;
  121.     uint status[7];
  122.  
  123.     gs_cachestatus(ifont_dir, status);
  124.     push(7);
  125.     make_uint_array(op - 6, status, 7);
  126.     return 0;
  127. }
  128.  
  129. /* <blimit> setcachelimit - */
  130. private int
  131. zsetcachelimit(i_ctx_t *i_ctx_p)
  132. {
  133.     os_ptr op = osp;
  134.  
  135.     check_int_leu(*op, max_uint);
  136.     gs_setcachelimit(ifont_dir, (uint) op->value.intval);
  137.     pop(1);
  138.     return 0;
  139. }
  140.  
  141. /* <mark> <size> <lower> <upper> setcacheparams - */
  142. private int
  143. zsetcacheparams(i_ctx_t *i_ctx_p)
  144. {
  145.     os_ptr op = osp;
  146.     uint params[3];
  147.     int i, code;
  148.     os_ptr opp = op;
  149.  
  150.     for (i = 0; i < 3 && !r_has_type(opp, t_mark); i++, opp--) {
  151.     check_int_leu(*opp, max_uint);
  152.     params[i] = opp->value.intval;
  153.     }
  154.     switch (i) {
  155.     case 3:
  156.         if ((code = gs_setcachesize(ifont_dir, params[2])) < 0)
  157.         return code;
  158.     case 2:
  159.         if ((code = gs_setcachelower(ifont_dir, params[1])) < 0)
  160.         return code;
  161.     case 1:
  162.         if ((code = gs_setcacheupper(ifont_dir, params[0])) < 0)
  163.         return code;
  164.     case 0:;
  165.     }
  166.     return zcleartomark(i_ctx_p);
  167. }
  168.  
  169. /* - currentcacheparams <mark> <size> <lower> <upper> */
  170. private int
  171. zcurrentcacheparams(i_ctx_t *i_ctx_p)
  172. {
  173.     os_ptr op = osp;
  174.     uint params[3];
  175.  
  176.     params[0] = gs_currentcachesize(ifont_dir);
  177.     params[1] = gs_currentcachelower(ifont_dir);
  178.     params[2] = gs_currentcacheupper(ifont_dir);
  179.     push(4);
  180.     make_mark(op - 3);
  181.     make_uint_array(op - 2, params, 3);
  182.     return 0;
  183. }
  184.  
  185. /* <font> .registerfont - */
  186. private int
  187. zregisterfont(i_ctx_t *i_ctx_p)
  188. {
  189.     os_ptr op = osp;
  190.     gs_font *pfont;
  191.     int code = font_param(op, &pfont);
  192.  
  193.     if (code < 0)
  194.     return code;
  195.     pfont->is_resource = true;
  196.     pop(1);
  197.     return 0;
  198. }
  199.  
  200. /* ------ Initialization procedure ------ */
  201.  
  202. const op_def zfont_op_defs[] =
  203. {
  204.     {"0currentfont", zcurrentfont},
  205.     {"2makefont", zmakefont},
  206.     {"2scalefont", zscalefont},
  207.     {"1setfont", zsetfont},
  208.     {"0cachestatus", zcachestatus},
  209.     {"1setcachelimit", zsetcachelimit},
  210.     {"1setcacheparams", zsetcacheparams},
  211.     {"0currentcacheparams", zcurrentcacheparams},
  212.     {"1.registerfont", zregisterfont},
  213.     op_def_end(zfont_init)
  214. };
  215.  
  216. /* ------ Subroutines ------ */
  217.  
  218. /* Validate a font parameter. */
  219. int
  220. font_param(const ref * pfdict, gs_font ** ppfont)
  221. {    /*
  222.      * Check that pfdict is a read-only dictionary, that it has a FID
  223.      * entry whose value is a fontID, and that the fontID points to a
  224.      * gs_font structure whose associated PostScript dictionary is
  225.      * pfdict.
  226.      */
  227.     ref *pid;
  228.     gs_font *pfont;
  229.     const font_data *pdata;
  230.  
  231.     check_type(*pfdict, t_dictionary);
  232.     if (dict_find_string(pfdict, "FID", &pid) <= 0 ||
  233.     !r_has_type(pid, t_fontID)
  234.     )
  235.     return_error(e_invalidfont);
  236.     pfont = r_ptr(pid, gs_font);
  237.     pdata = pfont->client_data;
  238.     if (!obj_eq(&pdata->dict, pfdict))
  239.     return_error(e_invalidfont);
  240.     *ppfont = pfont;
  241.     if (pfont == 0)
  242.     return_error(e_invalidfont);    /* unregistered font */
  243.     return 0;
  244. }
  245.  
  246. /* Add the FID entry to a font dictionary. */
  247. /* Note that i_ctx_p may be NULL. */
  248. int
  249. add_FID(i_ctx_t *i_ctx_p, ref * fp /* t_dictionary */ , gs_font * pfont,
  250.     gs_ref_memory_t *imem)
  251. {
  252.     ref fid;
  253.  
  254.     make_tav(&fid, t_fontID,
  255.          a_readonly | imemory_space(imem) | imemory_new_mask(imem),
  256.          pstruct, (void *)pfont);
  257.     return (i_ctx_p ? idict_put_string(fp, "FID", &fid) :
  258.         dict_put_string(fp, "FID", &fid, NULL));
  259. }
  260.  
  261. /* Make a transformed font (common code for makefont/scalefont). */
  262. private int
  263. make_font(i_ctx_t *i_ctx_p, const gs_matrix * pmat)
  264. {
  265.     os_ptr op = osp;
  266.     os_ptr fp = op - 1;
  267.     gs_font *oldfont, *newfont;
  268.     int code;
  269.     ref *pencoding = 0;
  270.  
  271.     code = font_param(fp, &oldfont);
  272.     if (code < 0)
  273.     return code;
  274.     {
  275.     uint space = ialloc_space(idmemory);
  276.  
  277.     ialloc_set_space(idmemory, r_space(fp));
  278.     if (dict_find_string(fp, "Encoding", &pencoding) > 0 &&
  279.         !r_is_array(pencoding)
  280.         )
  281.         code = gs_note_error(e_invalidfont);
  282.     else {
  283.         /*
  284.          * Temporarily substitute the new dictionary
  285.          * for the old one, in case the Encoding changed.
  286.          */
  287.         ref olddict;
  288.  
  289.         olddict = *pfont_dict(oldfont);
  290.         *pfont_dict(oldfont) = *fp;
  291.         code = gs_makefont(ifont_dir, oldfont, pmat, &newfont);
  292.         *pfont_dict(oldfont) = olddict;
  293.     }
  294.     ialloc_set_space(idmemory, space);
  295.     }
  296.     if (code < 0)
  297.     return code;
  298.     /*
  299.      * We have to allow for the possibility that the font's Encoding
  300.      * is different from that of the base font.  Note that the
  301.      * font_data of the new font was simply copied from the old one.
  302.      */
  303.     if (pencoding != 0 &&
  304.     !obj_eq(pencoding, &pfont_data(newfont)->Encoding)
  305.     ) {
  306.     if (newfont->FontType == ft_composite)
  307.         return_error(e_rangecheck);
  308.     /* We should really do validity checking here.... */
  309.     ref_assign(&pfont_data(newfont)->Encoding, pencoding);
  310.     lookup_gs_simple_font_encoding((gs_font_base *) newfont);
  311.     }
  312.     *fp = *pfont_dict(newfont);
  313.     pop(1);
  314.     return 0;
  315. }
  316. /* Create the transformed font dictionary. */
  317. /* This is the make_font completion procedure for all non-composite fonts */
  318. /* created at the interpreter level (see build_gs_simple_font in zbfont.c.) */
  319. int
  320. zbase_make_font(gs_font_dir * pdir, const gs_font * oldfont,
  321.         const gs_matrix * pmat, gs_font ** ppfont)
  322. {
  323.     /*
  324.      * We must call gs_base_make_font so that the XUID gets copied
  325.      * if necessary.
  326.      */
  327.     int code = gs_base_make_font(pdir, oldfont, pmat, ppfont);
  328.  
  329.     if (code < 0)
  330.     return code;
  331.     return zdefault_make_font(pdir, oldfont, pmat, ppfont);
  332. }
  333. int
  334. zdefault_make_font(gs_font_dir * pdir, const gs_font * oldfont,
  335.            const gs_matrix * pmat, gs_font ** ppfont)
  336. {
  337.     gs_font *newfont = *ppfont;
  338.     gs_memory_t *mem = newfont->memory;
  339.     /* HACK: we know this font was allocated by the interpreter. */
  340.     gs_ref_memory_t *imem = (gs_ref_memory_t *)mem;
  341.     ref *fp = pfont_dict(oldfont);
  342.     font_data *pdata;
  343.     ref newdict, newmat, scalemat;
  344.     uint dlen = dict_maxlength(fp);
  345.     uint mlen = dict_length(fp) + 3;    /* FontID, OrigFont, ScaleMatrix */
  346.     int code;
  347.  
  348.     if (dlen < mlen)
  349.     dlen = mlen;
  350.     if ((pdata = gs_alloc_struct(mem, font_data, &st_font_data,
  351.                  "make_font(font_data)")) == 0
  352.     )
  353.     return_error(e_VMerror);
  354.     /*
  355.      * This dictionary is newly created: it's safe to pass NULL as the
  356.      * dstack pointer to dict_copy and dict_put_string.
  357.      */
  358.     if ((code = dict_alloc(imem, dlen, &newdict)) < 0 ||
  359.     (code = dict_copy(fp, &newdict, NULL)) < 0 ||
  360.     (code = gs_alloc_ref_array(imem, &newmat, a_all, 12,
  361.                    "make_font(matrices)")) < 0
  362.     )
  363.     return code;
  364.     refset_null_new(newmat.value.refs, 12, imemory_new_mask(imem));
  365.     ref_assign(&scalemat, &newmat);
  366.     r_set_size(&scalemat, 6);
  367.     scalemat.value.refs += 6;
  368.     /*
  369.      * Create the scaling matrix.  We could do this several different
  370.      * ways: by "dividing" the new FontMatrix by the base FontMatrix, by
  371.      * multiplying the current scaling matrix by a ScaleMatrix kept in
  372.      * the gs_font, or by multiplying the current scaling matrix by the
  373.      * ScaleMatrix from the font dictionary.  We opt for the last of
  374.      * these.
  375.      */
  376.     {
  377.     gs_matrix scale, prev_scale;
  378.     ref *ppsm;
  379.  
  380.     if (!(dict_find_string(fp, "ScaleMatrix", &ppsm) > 0 &&
  381.           read_matrix(ppsm, &prev_scale) >= 0 &&
  382.           gs_matrix_multiply(pmat, &prev_scale, &scale) >= 0)
  383.         )
  384.         scale = *pmat;
  385.     write_matrix_new(&scalemat, &scale, imem);
  386.     }
  387.     r_clear_attrs(&scalemat, a_write);
  388.     r_set_size(&newmat, 6);
  389.     write_matrix_new(&newmat, &newfont->FontMatrix, imem);
  390.     r_clear_attrs(&newmat, a_write);
  391.     if ((code = dict_put_string(&newdict, "FontMatrix", &newmat, NULL)) < 0 ||
  392.     (code = dict_put_string(&newdict, "OrigFont", pfont_dict(oldfont->base), NULL)) < 0 ||
  393.     (code = dict_put_string(&newdict, "ScaleMatrix", &scalemat, NULL)) < 0 ||
  394.     (code = add_FID(NULL, &newdict, newfont, imem)) < 0
  395.     )
  396.     return code;
  397.     newfont->client_data = pdata;
  398.     *pdata = *pfont_data(oldfont);
  399.     pdata->dict = newdict;
  400.     r_clear_attrs(dict_access_ref(&newdict), a_write);
  401.     return 0;
  402. }
  403.  
  404. /* Convert an array of (unsigned) integers to stack form. */
  405. private void
  406. make_uint_array(register os_ptr op, const uint * intp, int count)
  407. {
  408.     int i;
  409.  
  410.     for (i = 0; i < count; i++, op++, intp++)
  411.     make_int(op, *intp);
  412. }
  413.  
  414. /* Remove scaled font and character cache entries that would be */
  415. /* invalidated by a restore. */
  416. private bool
  417. purge_if_name_removed(cached_char * cc, void *vsave)
  418. {
  419.     return alloc_name_index_is_since_save(cc->code, vsave);
  420. }
  421. void
  422. font_restore(const alloc_save_t * save)
  423. {
  424.     gs_font_dir *pdir = ifont_dir;
  425.  
  426.     if (pdir == 0)        /* not initialized yet */
  427.     return;
  428.  
  429.     /* Purge original (unscaled) fonts. */
  430.  
  431.     {
  432.     gs_font *pfont;
  433.  
  434. otop:
  435.     for (pfont = pdir->orig_fonts; pfont != 0;
  436.          pfont = pfont->next
  437.         ) {
  438.         if (alloc_is_since_save((char *)pfont, save)) {
  439.         gs_purge_font(pfont);
  440.         goto otop;
  441.         }
  442.     }
  443.     }
  444.  
  445.     /* Purge cached scaled fonts. */
  446.  
  447.     {
  448.     gs_font *pfont;
  449.  
  450. top:
  451.     for (pfont = pdir->scaled_fonts; pfont != 0;
  452.          pfont = pfont->next
  453.         ) {
  454.         if (alloc_is_since_save((char *)pfont, save)) {
  455.         gs_purge_font(pfont);
  456.         goto top;
  457.         }
  458.     }
  459.     }
  460.  
  461.     /* Purge xfonts and uncached scaled fonts. */
  462.  
  463.     {
  464.     cached_fm_pair *pair;
  465.     uint n;
  466.  
  467.     for (pair = pdir->fmcache.mdata, n = pdir->fmcache.mmax;
  468.          n > 0; pair++, n--
  469.         )
  470.         if (!fm_pair_is_free(pair)) {
  471.         if ((uid_is_XUID(&pair->UID) &&
  472.              alloc_is_since_save((char *)pair->UID.xvalues,
  473.                      save))
  474.             ) {
  475.             gs_purge_fm_pair(pdir, pair, 0);
  476.             continue;
  477.         }
  478.         if (pair->font != 0 &&
  479.             alloc_is_since_save((char *)pair->font, save)
  480.             ) {
  481.             if (!uid_is_valid(&pair->UID)) {
  482.             gs_purge_fm_pair(pdir, pair, 0);
  483.             continue;
  484.             }
  485.             /* Don't discard pairs with a surviving UID. */
  486.             pair->font = 0;
  487.         }
  488.         if (pair->xfont != 0 &&
  489.             alloc_is_since_save((char *)pair->xfont, save)
  490.             )
  491.             gs_purge_fm_pair(pdir, pair, 1);
  492.         }
  493.     }
  494.  
  495.     /* Purge characters with names about to be removed. */
  496.     /* We only need to do this if any new names have been created */
  497.     /* since the save. */
  498.  
  499.     if (alloc_any_names_since_save(save))
  500.     gx_purge_selected_cached_chars(pdir, purge_if_name_removed,
  501.                        (void *)save);
  502.  
  503. }
  504.